<?php
/*************************************************

理财列表接口 - the
Author: Wanghuabin <17-10-31>

*************************************************/
namespace Home\Controller;
use Think\Controller;
class FinancialController extends Controller {

    public function index() {
        $currentPage = I('post.currentPage',1,int);
        $pageSize = I('post.pageSize',3,int);
        $typeTwo = I('post.typeTwo');
        $typeOne = I('post.typeOne');
        $page = ($currentPage-1)*$pageSize;
            if($typeOne==2) {
                //精选平台
                $map['is_hot'] = 1;
            } else {
                //热门推荐
                $map['is_hot'] = 0;
            }

            if($typeTwo==2) {
                //首投返利
                $map['user_award'] = array('gt',0);
            } else if($typeTwo==3) {
                //复投加息
                $map['rebate'] = array('gt',0);
            } else if($typeTwo==4) {
                //多投多返roi
                $map['user_roi'] = array('gt',0);
            }

            $map['status'] = 0;
            $platform = D('Platform')-> get_all_page($map,$page,$pageSize,$order='`order` asc');
            $map = array();
            if($platform){
                foreach($platform as $k => $v) {
                    $map['platform_id'] = $v['id'];
                    $order = "deadline asc";
                    $plat_sch = D('PlatformScheme')->get_one($map,$field='',$order);
                    $platform[$k]['sch_start_amount'] = $plat_sch['start_amount'];
                    $platform[$k]['sch_user_roi'] = $plat_sch['start_amount']*$plat_sch['user_roi']/100;
                    $plat_sch_count = D('PlatformObject')->count($map);
                    $platform[$k]['plat_sch_count'] = $plat_sch_count;
                    $pic = $v['pic'];
                    $platform[$k]['pic'] = preg_replace ('/http:\/\//', 'https://', $pic,1);

                }
                $res['errorCode'] = 1;
                $res['msg'] ='成功';
                $res['data'] = $platform;
            }else{
                $res['errorCode'] = 0;
                $res['msg'] ='暂无数据';
            }

            $this->ajaxReturn($res);
    }


    public function new_index() {
        //广告位
        $api_lc_top = R('Ad/get_ad',array('tag'=>'api_lc_top'));
        $map['status']=0;
        $plat_list = D('Platform')->get_all($map);

        foreach ($plat_list as $k => $v) {
              //统计平台投资方案数
            $map = array();
            $map['platform_id'] = $v['id'];
            $plat_list[$k]['scheme_count']=D('PlatformScheme')->count($map);
            $start_amount = (int)$v['start_amount'];
            $count = strlen($start_amount);
            $start_num = substr($start_amount,0,1);
            if($count==2){
                $plat_list[$k]['start_num'] = $start_num."<span>十元起</span>";
            }else if($count==3){
                $plat_list[$k]['start_num'] = $start_num."<span>千元起</span>";
            }else if($count==4){
                $plat_list[$k]['start_num'] = $start_num."<span>千元起</span>";
            }else if($count==5){
                $plat_list[$k]['start_num'] = $start_num."<span>万元起</span>";
            }

            if(strpos($v['start_deadline'],"个月")){
                $start_day = str_replace("个月","",$v['start_deadline']);
                $plat_list[$k]['start_day'] =$start_day."<span>个月起</span>";
            }else if(strpos($v['start_deadline'],"天")){
                $start_day = str_replace("天","",$v['start_deadline']);
                $plat_list[$k]['start_day'] =$start_day."<span>天起</span>";
            }
            $map = array();
            $map['platform_id'] = $v['id'];
            $circle = D('Circle')->get_one($map);
            if($circle){
                $plat_list[$k]['circle'] = $circle;
            }else{
                unset($plat_list[$k]);
            }

        }
        if($api_lc_top && $plat_list) {
            $data = array('errorCode'=>1,'msg'=>"获取成功！",'api_lc_top'=>$api_lc_top,'list'=>array_merge($plat_list));
        } else {
            $data = array('errorCode'=>0,'msg'=>"暂无数据！");
        }
        $this->ajaxReturn($data);

    }



    //活动页（红包详情页）
    public function event(){
        //判断是否是红包页面
        $packet = I('get.packet');
        $id = I('get.platform_id');//平台id

        $map['id'] = $id;
        $info = D('Platform')->get_one($map);
        $map = array();
        $map['platform_id'] = $id;
        $info['circle'] = D('Circle')->get_one($map);
        $info['cricel_leve']= R('Circle/get_cricel_leve',array('platform_id'=>$id));//获取圈子评级
       // print_r($info);
        $event_years_top = 0;
        if($packet){
            $list = R('Packet/get_redpacket',array('user_id'=>$packet,'platform_id'=>$id));
           foreach ($list['scheme'] as $k => $v) {
                if($v['deadline_unit']==0){
                    $tz_award= $v['start_amount']*$v['yields']/100/12*$v['deadline'];
                    $tz_all_award=sprintf("%.2f", $tz_award+$v['p_user_award']);
                    $tz_award = sprintf("%.2f",$tz_award);
                    $list['scheme'][$k]['tz_award'] = $tz_award;
                    $list['scheme'][$k]['tz_all_award']  = $tz_all_award;
                    $event_years =  sprintf("%.2f",$tz_all_award/$v['start_amount']*12/$v['deadline'])*100;
                    if($event_years>$event_years_top){
                        $event_years_top = $event_years;
                    }
                    $list['scheme'][$k]['event_years'] = $event_years;
                }else{
                    $tz_award= $v['start_amount']*$v['yields']/100/360*$v['deadline'];
                    $tz_all_award=sprintf("%.2f", $tz_award+$v['p_user_award']);
                    $tz_award = sprintf("%.2f",$tz_award);
                    $list['scheme'][$k]['tz_award'] = $tz_award;
                    $list['scheme'][$k]['tz_all_award']  = $tz_all_award;
                    $event_years =  sprintf("%.2f",$tz_all_award/$v['start_amount']*12/$v['deadline'])*100;
                    $list['scheme'][$k]['event_years'] = $event_years;
                    if($event_years>$event_years_top){
                        $event_years_top = $event_years;
                    }
                }

            }
        }else{

            //默认的理财详情

            //投资方案
            $map = array();
            $map['platform_id'] = $id;
            $list = D('PlatformScheme')->get_all($map);
            foreach ($list as $k => $v) {
                if($v['deadline_unit']==0){
                    $tz_award= $v['start_amount']*$v['yields']/100/12*$v['deadline'];
                    $tz_all_award=sprintf("%.2f", $tz_award+$v['user_award']);
                    $tz_award = sprintf("%.2f",$tz_award);
                    $list[$k]['tz_award'] = $tz_award;
                    $list[$k]['tz_all_award']  = $tz_all_award;
                    $event_years=  sprintf("%.2f",$tz_all_award/$v['start_amount']*12/$v['deadline'])*100;
                    $list[$k]['event_years'] = $event_years;
                    if($event_years>$event_years_top){

                        $event_years_top = $event_years;
                    }
                }else{
                    $tz_award= $v['start_amount']*$v['yields']/100/360*$v['deadline'];
                    $tz_all_award=sprintf("%.2f", $tz_award+$v['user_award']);
                    $tz_award = sprintf("%.2f",$tz_award);
                    $list[$k]['tz_award'] = $tz_award;
                    $list[$k]['tz_all_award']  = $tz_all_award;
                    $event_years=  sprintf("%.2f",$tz_all_award/$v['start_amount']*12/$v['deadline'])*100;
                    $list[$k]['event_years'] = $event_years;
                    if($event_years>$event_years_top){
                        $event_years_top = $event_years;
                    }
                }

            }

        }
        if($event_years_top && $info && $list) {
            $data = array('errorCode'=>1,'msg'=>"获取成功！",'event_years_top'=>$event_years_top,'list'=>$list,'info'=>$info);
        } else {
            $data = array('errorCode'=>0,'msg'=>"暂无数据！");
        }
        $this->ajaxReturn($data);

    }




}